﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VINETLIFE.BL;
using VINETLIFE.DO;
using VINETLIFE.Data;
using System.Data;
using VINETLIFE.AdminCP;
using System.Web.UI.HtmlControls;

namespace VINETLIFE.Customers
{
    public partial class DistributeInfo : System.Web.UI.Page
    {
        cmsCustomerDO objCus = new cmsCustomerDO();
        cmsCustomerBL cusBL = new cmsCustomerBL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["CustomerId"] != null)
            {
                objCus.Id = int.Parse(Session["CustomerId"].ToString());
                if (!IsPostBack)
                {
                    SEO();
                    InitForm();
                }
            }
            else
            {
                Functions.Alert("Bạn không có quyền truy cập vào khi chưa đăng nhập, vui lòng đăng nhập", "/dang-nhap/");
            }
        }

        private void SEO()
        {
            Page.Title = "Thông tin cá nhân | " + Ultility.WebTitle();

            sysConfigDO objConfig = new sysConfigDO();
            objConfig.ConfigID = 2;

            objConfig = new sysConfigBL().Select(objConfig);


            HtmlMeta metaKey = new HtmlMeta();
            metaKey.Name = "keywords";
            metaKey.Content = objConfig.ConfigValue;
            Page.Header.Controls.Add(metaKey);


            objConfig.ConfigID = 3;
            objConfig = new sysConfigBL().Select(objConfig);
            HtmlMeta metaDesc = new HtmlMeta();
            metaDesc.Name = "description";
            metaDesc.Content = objConfig.ConfigValue;
            Page.Header.Controls.Add(metaDesc);
        }
        private void InitForm()
        {
            LinqVinetlifeDataContext vinetlifeLinq = new LinqVinetlifeDataContext();
            try
            {
                string nguoiBaoTro = "---";
                objCus = cusBL.Select(objCus);
                lblCMTND.Text = objCus.Identification;
                var queryIntroduction = from p in vinetlifeLinq.cmsCustomers
                                        where p.Id == objCus.IntroductionCustomerId
                                        select p;
                if (queryIntroduction.Count() > 0)
                {

                    nguoiBaoTro = queryIntroduction.FirstOrDefault().CustomerCode + " - " + queryIntroduction.FirstOrDefault().FullName;
                    if (string.IsNullOrEmpty(nguoiBaoTro))
                        nguoiBaoTro = "---";

                    if (!string.IsNullOrEmpty(queryIntroduction.FirstOrDefault().IntroductionCustomerCode))
                    {
                        lblCapQuanLy.Text = "1";
                    }
                    else
                    {
                        lblCapQuanLy.Text = "0";
                    }
                }
                else
                {
                    nguoiBaoTro = "---";
                    lblCapQuanLy.Text = "0";
                }
                lblNguoiBaoTro.Text = nguoiBaoTro;
                lblNgayDangKy.Text = string.Format("{0:dd/MM/yyyy}", objCus.CreatedDate);
                lblMaSoCaNhan.Text = objCus.CustomerCode;

                var query = from p in vinetlifeLinq.cmsCustomers
                            where p.IntroductionCustomerId == objCus.Id
                            select p;
                if (query.Count() > 0)
                {
                    lblSoLuongKhachHang.Text = query.Count().ToString();
                    lblSoMa.Text = query.Count().ToString();
                }

                var queryCount = from p in vinetlifeLinq.cmsAccountReceiveMoneys
                                 where p.ToCustomerId == objCus.Id && (p.Status.Equals("OK") || p.Status.Equals("RECEIVED")) && p.CreatedDate.Value.Month == DateTime.Now.Month
                                 select p;
                if (queryCount.Count() > 0)
                {

                    long value = 0;
                    foreach (var item in queryCount)
                    {
                        value += item.ReceivedValue.Value;
                    }

                    lblThangNay.Text = string.Format("{0:#,##}", value) + " đ";
                }
            }
            catch { }
        }


        protected void btnHuy_Click(object sender, EventArgs e)
        {
            InitForm();
        }


    }
}
